{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# New Stuff!\n", "\n", "## New Assembly Directives\n", "\n", "* .BLKW - block of memory; takes decimal or hex only\n", "* .FILL - a memory location; decimal or hex only\n", "* .STRINGZ - a zero-padded, zero-terminated ASCII \"string\"" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Assembled! Use %dis or %dump to examine; use %exe to run.\n" ] } ], "source": [ ".ORIG x3000\n", ".BLKW 1 ; simple variable (or .FILL 0)\n", ".FILL x1234 ; simple initialized variable\n", ".BLKW 10 ; array of ten ints (initialized to 0)\n", ".FILL xFFFF ; simple initialized variable\n", ".END" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "============================================================\n", "Memory dump:\n", "============================================================\n", " x3000: x0000\n", " x3001: x1234\n", " x3002: x0000\n", " x3003: x0000\n", " x3004: x0000\n", " x3005: x0000\n", " x3006: x0000\n", " x3007: x0000\n", " x3008: x0000\n", " x3009: x0000\n", " x300A: x0000\n", " x300B: x0000\n", " x300C: xFFFF\n" ] } ], "source": [ "%dump x3000 x300C" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Assembled! Use %dis or %dump to examine; use %exe to run.\n" ] } ], "source": [ ".ORIG x3100\n", ".STRINGZ \"abcdefg\"\n", ".END" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "============================================================\n", "Memory dump:\n", "============================================================\n", " x3100: x0061\n", " x3101: x0062\n", " x3102: x0063\n", " x3103: x0064\n", " x3104: x0065\n", " x3105: x0066\n", " x3106: x0067\n", " x3107: x0000\n", " x3108: x0037\n", " x3109: x0038\n", " x310A: x0000\n" ] } ], "source": [ "%dump x3100 x310A" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Assembled! Use %dis or %dump to examine; use %exe to run.\n" ] } ], "source": [ ".ORIG x3100\n", ".STRINGZ \"012345\"\n", ".STRINGZ \"678\"\n", ".END" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "============================================================\n", "Memory dump:\n", "============================================================\n", " x3100: x0030\n", " x3101: x0031\n", " x3102: x0032\n", " x3103: x0033\n", " x3104: x0034\n", " x3105: x0035\n", " x3106: x0000\n", " x3107: x0036\n", " x3108: x0037\n", " x3109: x0038\n", " x310A: x0000\n" ] } ], "source": [ "%dump x3100 x310A" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## New TRAP Instructions\n", "\n", "TRAP instructions are called \"service calls\" because they \"call\" a service of the Operating System. Generally, thay are composed of:\n", "\n", "Opcode | 0000 | Trapvector\n", "-------|------|------------\n", "1111 | 0000 | 0000 0000\n", "\n", "The Trapvector is 8 bits that indicate a location in memory. Of course the LC-3 has 16 bit addresses, but we only specify 8 here. \n", "\n", "* 1111 0000 0010 0011 - x23 is IN\n", "* 1111 0000 0010 0001 - x21 is OUT" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "============================================================\n", "Memory dump:\n", "============================================================\n", " x0000: x0495\n", " x0001: x0495\n", " x0002: x0495\n", " x0003: x0495\n", " x0004: x0495\n", " x0005: x0495\n", " x0006: x0495\n", " x0007: x0495\n", " x0008: x0495\n", " x0009: x0495\n", " x000A: x0495\n", " x000B: x0495\n", " x000C: x0495\n", " x000D: x0495\n", " x000E: x0495\n", " x000F: x0495\n", " x0010: x0495\n", " x0011: x0495\n", " x0012: x0495\n", " x0013: x0495\n", " x0014: x0495\n", " x0015: x0495\n", " x0016: x0495\n", " x0017: x0495\n", " x0018: x0495\n", " x0019: x0495\n", " x001A: x0495\n", " x001B: x0495\n", " x001C: x0495\n", " x001D: x0495\n", " x001E: x0495\n", " x001F: x0495\n", " x0020: x044C\n", " x0021: x0450\n", " x0022: x0456\n", " x0023: x0463\n", " x0024: x046F\n", " x0025: x048E\n", " x0026: x0495\n", " x0027: x0495\n", " x0028: x0495\n", " x0029: x0495\n", " x002A: x0495\n", " x002B: x0495\n", " x002C: x0495\n", " x002D: x0495\n", " x002E: x0495\n", " x002F: x0495\n", " x0030: x0495\n", " x0031: x0495\n", " x0032: x0495\n", " x0033: x0495\n", " x0034: x0495\n", " x0035: x0495\n", " x0036: x0495\n", " x0037: x0495\n", " x0038: x0495\n", " x0039: x0495\n", " x003A: x0495\n", " x003B: x0495\n", " x003C: x0495\n", " x003D: x0495\n", " x003E: x0495\n", " x003F: x0495\n", " x0040: x0495\n", " x0041: x0495\n", " x0042: x0495\n", " x0043: x0495\n", " x0044: x0495\n", " x0045: x0495\n", " x0046: x0495\n", " x0047: x0495\n", " x0048: x0495\n", " x0049: x0495\n", " x004A: x0495\n", " x004B: x0495\n", " x004C: x0495\n", " x004D: x0495\n", " x004E: x0495\n", " x004F: x0495\n", " x0050: x0495\n", " x0051: x0495\n", " x0052: x0495\n", " x0053: x0495\n", " x0054: x0495\n", " x0055: x0495\n", " x0056: x0495\n", " x0057: x0495\n", " x0058: x0495\n", " x0059: x0495\n", " x005A: x0495\n", " x005B: x0495\n", " x005C: x0495\n", " x005D: x0495\n", " x005E: x0495\n", " x005F: x0495\n", " x0060: x0495\n", " x0061: x0495\n", " x0062: x0495\n", " x0063: x0495\n" ] } ], "source": [ "%dump x0000" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " PC <= x3200\n", " memory[x3200] <= xF023\n", " PC <= x3201\n", " memory[x3201] <= xF025\n", " PC <= x3202\n", "Assembled! Use %dis or %dump to examine; use %exe to run.\n" ] } ], "source": [ ".ORIG x3200\n", "1111 0000 0010 0011\n", "1111 0000 0010 0101\n", ".END" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " PC <= x3200\n", "\n", "============================================================\n", "Registers:\n", "============================================================\n", "PC: x3200\n", "N: 0 Z: 1 P: 0 \n", "R0: x0000 R1: x0000 R2: x0000 R3: x0000 \n", "R4: x0000 R5: x0000 R6: x0000 R7: x0000 \n" ] } ], "source": [ "%pc x3200" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "Input a character> a\n", "a\n", "============================================================\n", "Computation completed\n", "============================================================\n", "Instructions: 260\n", "Cycles: 2166 (0.001083 milliseconds)\n", "\n", "============================================================\n", "Registers:\n", "============================================================\n", "PC: x048E\n", "N: 0 Z: 0 P: 1 \n", "R0: x0061 R1: x0000 R2: x0000 R3: x0000 \n", "R4: x0000 R5: x0000 R6: x0000 R7: x3202 \n" ] } ], "source": [ "%cont" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Assembled! Use %dis or %dump to examine; use %exe to run.\n" ] } ], "source": [ ".ORIG x3200\n", "1111 0000 0010 0011\n", "1111 0000 0010 0001\n", "1111 0000 0010 0101\n", ".END" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "Input a character> %\n", "%\n", "%============================================================\n", "Computation completed\n", "============================================================\n", "Instructions: 267\n", "Cycles: 2230 (0.001115 milliseconds)\n", "\n", "============================================================\n", "Registers:\n", "============================================================\n", "PC: x048E\n", "N: 0 Z: 1 P: 0 \n", "R0: x0025 R1: x0000 R2: x0000 R3: x0000 \n", "R4: x0000 R5: x0000 R6: x0000 R7: x3203 \n" ] } ], "source": [ "%exe" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## All of the TRAP Instructions\n", "\n", "Detailed description on page 543.\n", "\n", "* x20: GETC - no echo\n", "* x21: OUT - ouput character\n", "* x22: PUTS - output string\n", "* x23: IN - get a character\n", "* x24: PUTSP - two chars per word, [7:0] right-first, then left [15:8]\n", "* x25: HALT - infinite loop" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Practice\n", "\n", "* Q1: Write the standard \"Hello, World!\" program in machine language\n", "* Q2: Get a number (ASCII character) from the user and add 1 to it; show result\n", "* Q3: Get a number (ASCII character, 0 - 4) from the user and double it; show result\n", "* Q4: display a -1 on the screen\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Q1:**" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Assembled! Use %dis or %dump to examine; use %exe to run.\n" ] } ], "source": [ ".ORIG x3000\n", "1110 000 000000010 ;; Put address of \"Hello, World!\" in R0\n", "1111 0000 0010 0010 ;; PUTS\n", "1111 0000 0010 0101 ;; HALT\n", ".STRINGZ \"Hello, world!\" ;; DATA\n", ".END" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello, world!============================================================\n", "Computation completed\n", "============================================================\n", "Instructions: 156\n", "Cycles: 1291 (0.000646 milliseconds)\n", "\n", "============================================================\n", "Registers:\n", "============================================================\n", "PC: x048E\n", "N: 0 Z: 0 P: 1 \n", "R0: x3003 R1: x0000 R2: x0000 R3: x0000 \n", "R4: x0000 R5: x0000 R6: x0000 R7: x3003 \n" ] } ], "source": [ "%exe" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Q2:**" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Assembled! Use %dis or %dump to examine; use %exe to run.\n" ] } ], "source": [ ".ORIG x3100\n", "1111 0000 0010 0000 ;; GETC\n", "0001 000 000 1 00001 ;; ADD\n", "1111 0000 0010 0001 ;; OUT\n", "1111 0000 0010 0101 ;; HALT\n", ".END" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2\n", "3============================================================\n", "Computation completed\n", "============================================================\n", "Instructions: 14\n", "Cycles: 124 (0.000062 milliseconds)\n", "\n", "============================================================\n", "Registers:\n", "============================================================\n", "PC: x048E\n", "N: 0 Z: 1 P: 0 \n", "R0: x0033 R1: x0000 R2: x0000 R3: x0000 \n", "R4: x0000 R5: x0000 R6: x0000 R7: x3104 \n" ] } ], "source": [ "%exe" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Q3:**" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Assembled! Use %dis or %dump to examine; use %exe to run.\n" ] } ], "source": [ ".ORIG x3100\n", "1111 0000 0010 0000 ;; GETC into R0\n", "0101 001 001 1 00000 ;; put 0 in R1\n", "0001 001 001 1 1 0000 ;; put x30 in R1; ADD \n", "0001 001 001 1 1 0000 ;; put x30 in R1; ADD\n", "0001 001 001 1 1 0000 ;; put x30 in R1; ADD\n", "0001 010 001 1 00000 ;; put x30 in R2; ADD R2 <= R1 + 0\n", "1001 001 001 111111 ;; NOT R1\n", "0001 001 001 1 00001 ;; ADD 1\n", "0001 000 001 0 00 000 ;; ADD -30; R0 <= R1 + 0\n", ";; 1111 0000 0010 0001 ;; OUT\n", "1111 0000 0010 0101 ;; HALT\n", ".END" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "============================================================\n", "Registers:\n", "============================================================\n", "PC: x3100\n", "N: 0 Z: 1 P: 0 \n", "R0: x0000 R1: x0000 R2: x0000 R3: x0000 \n", "R4: x0000 R5: x0000 R6: x0000 R7: x0000 \n" ] } ], "source": [ "%pc x3100" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "============================================================\n", "Stepping... => read, <= write, (Instructions/Cycles):\n", "============================================================\n", " PC <= x3101\n", "(1/9) GETC [1] (x3101*: xF020)\n", " R7 <= x3101\n", " PC <= x044C\n", "0\n", " memory[xFE02] <= x0030\n", "\n", "============================================================\n", "Registers:\n", "============================================================\n", "PC: x044C\n", "N: 0 Z: 1 P: 0 \n", "R0: x0000 R1: x0000 R2: x0000 R3: x0000 \n", "R4: x0000 R5: x0000 R6: x0000 R7: x3101 \n" ] } ], "source": [ "%step" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "============================================================\n", "Stepping... => read, <= write, (Instructions/Cycles):\n", "============================================================\n", " PC <= x044D\n", "(2/21) LDI R0, x043E (x044D*: xA1F1)\n", " Reading memory[x043e] (xfe00) =>\n", " Reading memory[xfe00] (x0000) =>\n", " R0 <= x0000\n", " NZP <= (0, 1, 0)\n", "\n", "============================================================\n", "Registers:\n", "============================================================\n", "PC: x044D\n", "N: 0 Z: 1 P: 0 \n", "R0: x0000 R1: x0000 R2: x0000 R3: x0000 \n", "R4: x0000 R5: x0000 R6: x0000 R7: x3101 \n" ] } ], "source": [ "%step" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "============================================================\n", "Breakpoints\n", "============================================================\n", " 1) x3102: x1270\n" ] } ], "source": [ "%bp x3102" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "============================================================\n", "Computation completed\n", "============================================================\n", "Instructions: 2822118\n", "Cycles: 25399038 (12.699519 milliseconds)\n", "\n", "============================================================\n", "Registers:\n", "============================================================\n", "PC: x048E\n", "N: 0 Z: 0 P: 1 \n", "R0: x0060 R1: x0030 R2: xFFD0 R3: x0000 \n", "R4: x0000 R5: x0000 R6: x0000 R7: x310A \n" ] } ], "source": [ "%cont" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "============================================================\n", "Registers:\n", "============================================================\n", "PC: x3102\n", "N: 0 Z: 1 P: 0 \n", "R0: x0030 R1: x0000 R2: x0000 R3: x0000 \n", "R4: x0000 R5: x0000 R6: x0000 R7: x3101 \n" ] } ], "source": [ "%regs" ] }, { "cell_type": "code", "execution_count": 58, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "============================================================\n", "Stepping... => read, <= write, (Instructions/Cycles):\n", "============================================================\n", " PC <= x3109\n", "(13/93) ADD R0, R1, R0 [9] (x3109*: x1040)\n", " R0 <= x0012\n", " NZP <= (0, 0, 1)\n", "\n", "============================================================\n", "Registers:\n", "============================================================\n", "PC: x3109\n", "N: 0 Z: 0 P: 1 \n", "R0: x0012 R1: xFFE2 R2: x001E R3: x0000 \n", "R4: x0000 R5: x0000 R6: x0000 R7: x3101 \n" ] } ], "source": [ "%step" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Calysto LC3", "language": "asm", "name": "calysto_lc3" }, "language_info": { "file_extension": ".asm", "mimetype": "text/x-gas", "name": "gas" } }, "nbformat": 4, "nbformat_minor": 2 }